/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.cmp.arena.controls;

import com.jme3.math.Vector3f;
import com.jme3.renderer.RenderManager;
import com.jme3.renderer.ViewPort;
import com.jme3.scene.Node;
import com.jme3.scene.Spatial;
import com.jme3.scene.control.AbstractControl;
import static com.cmp.core.constants.UIDConstants.*;
import com.jme3.scene.control.Control;

/**
 * Handles ownership, spawning and possibly throwing of the ball.
 * lets make this reusable for all kinds of maps.
 * @author krummer
 */
public class CameraControl extends AbstractControl {
    private Node target;
    public CameraControl(Node target) {
        super();
        this.target = target;
    }
    
    @Override
    public void setSpatial(Spatial spatial) {
        super.setSpatial(spatial);
    }
    
    @Override
    protected void controlUpdate(float tpf) {
        //Vector3f pos = Vector3f.ZERO;
        //spatial.worldToLocal(((Spatial)spatial.getUserData("Target")).getWorldTranslation(), pos);
        Vector3f pos = target.getWorldTranslation().clone().setY(0.f);
        //CameraNode camNode = (CameraNode)((Node)spatial).getChild(0);
        //camNode.setLocalTranslation(0.f, 10.f, 10.f);
        Vector3f cursor = target.getUserData(CURSOR_POS_UID);
        Vector3f delta = Vector3f.ZERO;
        if(cursor != null) {
            delta = cursor.subtract(target.getWorldTranslation());
            delta.y = 0;
            float max_add = 10.0f;
            delta.x = max_add * Math.max(-1.f,Math.min(1.f,delta.x / 30.f));
            delta.z = max_add * Math.max(-1.f,Math.min(1.f,delta.z / 30.f));
        }
        spatial.setLocalTranslation(pos.add(delta));
    }

    @Override
    protected void controlRender(RenderManager rm, ViewPort vp) {
    }

    public Control cloneForSpatial(Spatial spatial) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
    
}
